import { Controller } from "../../mvc/core/Controller";
import { LadderBattlefieldIcon } from "../view/ladderBattlefield/LadderBattlefieldIcon";
import { IView } from "../../mvc/interfaces/IView";
import { LadderBattlefieldModule } from "../../../modules/LadderBattlefieldModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { NpcEffectRule } from "../rules/NpcEffectRule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { DataEvent } from "../events/DataEvent";
import { LadderBattlefieldUtil } from "../view/ladderBattlefield/LadderBattlefieldUtil";
import { EEntityFightMode } from "../../../Message/Public/EEntityFightMode";
import { Language } from "../../../extend/language/Language";
import { GameProxy } from "../mvc/GameProxy";
import { Alert } from "../../../com/mui/controls/Alert";
import { LadderBattlefieldTopListVo } from "../view/ladderBattlefield/vo/LadderBattlefieldTopListVo";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SLadderSchedule } from "../../../Message/Public/SLadderSchedule";
import { DateUtil } from "../../common/tools/DateUtil";
import { ClockManager } from "../manager/ClockManager";
import { SLadderMyInfo } from "../../../Message/Public/SLadderMyInfo";
import { ILadderIntegralBattlefieldModule } from "../../../modules/interfaces/ILadderIntegralBattlefieldModule";
import { SLadderToplists } from "../../../Message/Public/SLadderToplists";
import { SLadderSignUp } from "../../../Message/Public/SLadderSignUp";
import { GangFightsWaitingTimeView } from "../view/gangFights/GangFightsWaitingTimeView";
import { SLadderBattles } from "../../../Message/Public/SLadderBattles";
import { SLadderMatch } from "../../../Message/Public/SLadderMatch";
import { TimerAlertWin } from "../view/common/alertwins/TimerAlertWin";
import { GangFightsReadyTimeView } from "../view/gangFights/GangFightsReadyTimeView";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { GameController } from "../mvc/GameController";
import { SLadderStart } from "../../../Message/Public/SLadderStart";
import { SLadderResult } from "../../../Message/Public/SLadderResult";
import { SLadderPlayerInfo } from "../../../Message/Public/SLadderPlayerInfo";
import { LadderBattlefieldResultWindow } from "../view/ladderBattlefield/LadderBattlefieldResultWindow";
import { LeftTimeView } from "../view/common/LeftTimeView";
type int = number;
//class LadderBattlefieldController
    
    export  class LadderBattlefieldController extends Controller
    {
       

        /* internal  */get btn(): LadderBattlefieldIcon
        {
            if (!this._btn) 
            {
                this._btn = new LadderBattlefieldIcon();
            }
            return this._btn;
        }

        protected /* override */ initView(): IView
        {
            if (!this._module) 
            {
                this._module = new LadderBattlefieldModule();
                this._module.addEventListener(WindowEvent.SHOW, this.onShow);
                this._module.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._module;
        }

        /* internal  */onShow(arg1: WindowEvent): void
        {
            NpcEffectRule.registDialogEffectWindow(view);
            Dispatcher.addEventListener(EventName.LadderBattlefieldCareerApply, this.onCareerApplyHandler);
            Dispatcher.addEventListener(EventName.LadderBattlefieldGetToplist, this.onGetToplistHandler);
            Dispatcher.addEventListener(EventName.LadderBattlefieldSignUp, this.onSignUpHandler);
            Dispatcher.addEventListener(EventName.LadderBattlefieldGetBattles, this.onGetBattlesHandler);
            Dispatcher.addEventListener(EventName.LadderBattlefieldGetToplistByCareer, this.onGetToplistByCareerHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicLadderToplists, this.onLadderToplistsHandler);
            return;
        }

        /* internal  */onClose(arg1: WindowEvent): void
        {
            cache.ladderBattlefield.multiTabBarSelectIndex = -1;
            NpcEffectRule.unRegistDialogEffectWindow(view);
            Dispatcher.removeEventListener(EventName.LadderBattlefieldCareerApply, this.onCareerApplyHandler);
            Dispatcher.removeEventListener(EventName.LadderBattlefieldGetToplist, this.onGetToplistHandler);
            Dispatcher.removeEventListener(EventName.LadderBattlefieldSignUp, this.onSignUpHandler);
            Dispatcher.removeEventListener(EventName.LadderBattlefieldGetBattles, this.onGetBattlesHandler);
            Dispatcher.removeEventListener(EventName.LadderBattlefieldGetToplistByCareer, this.onGetToplistByCareerHandler);
            NetDispatcher.removeCmdListener(EPublicCommand._ECmdPublicLadderToplists, this.onLadderToplistsHandler);
            return;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.LadderBattlefieldIconClick, this.onIconClickHandler);
            Dispatcher.addEventListener(EventName.LadderBattlefieldEnter, this.onEnterHandler);
            Dispatcher.addEventListener(EventName.LadderBattlefieldLeave, this.onLeaveHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicLadderOpen, this.onLadderOpenHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicLadderClose, this.onLadderCloseHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicLadderMyInfo, this.onLadderMyInfoHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicLadderSignUp, this.onLadderSignUpHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicLadderBattles, this.onLadderBattlesHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicLadderMatch, this.onLadderMatchHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicLadderStart, this.onLadderStartHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicLadderResult, this.onLadderResultHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicLadderPlayerInfo, this.onLadderPlayerInfoHandler);
            return;
        }

        /* internal  */onIconClickHandler(arg1: DataEvent): void
        {
            var loc1=null;
            if (LadderBattlefieldUtil.isInLadderBattlefield) 
            {
                loc1 = cache.role.entityInfo.fightModel != EEntityFightMode._EEntityFightModeObseve ? Language.getString(28618) : Language.getString(90322);
                Dispatcher.dispatchEvent(new DataEvent(EventName.LadderBattlefieldLeave, {"needShowAlert": true, "str": loc1}));
            }
            else 
            {
                GameProxy.copy.enterLadder(true);
            }
            return;
        }

        /* internal  */onEnterHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.copy.enterLadder(false, loc1, 1);
            return;
        }

        /* internal  */onLeaveHandler(arg1: DataEvent): void
        {
            var e: DataEvent;
            var onBtnClickHandler: Function;

            var loc1;
            onBtnClickHandler = null;
            e = arg1;
            onBtnClickHandler = (arg1: int): void
            {
                if (arg1 == Alert.YES) 
                {
                    GameProxy.copy.enterLadder(true);
                }
                return;
            }
            if (e.data.needShowAlert) 
            {
                Alert.show(e.data.str, null, Alert.YES | Alert.NO, null, onBtnClickHandler);
            }
            else 
            {
                onBtnClickHandler(Alert.YES);
            }
            return;
        }

        /* internal  */onCareerApplyHandler(arg1: DataEvent): void
        {
            GameProxy.copy.setLadderCareer(arg1.data as int);
            return;
        }

        /* internal  */onGetToplistHandler(arg1: DataEvent): void
        {
            GameProxy.copy.getLadderToplist(arg1.data as LadderBattlefieldTopListVo);
            return;
        }

        /* internal  */onSignUpHandler(arg1: DataEvent): void
        {
            GameProxy.copy.signUpLadder(arg1.data as Boolean);
            return;
        }

        /* internal  */onGetBattlesHandler(arg1: DataEvent): void
        {
            GameProxy.copy.getLadderBattles();
            return;
        }

        /* internal  */onGetToplistByCareerHandler(arg1: DataEvent): void
        {
            cache.ladderBattlefield.multiTabBarSelectIndex = arg1.data as int;
            var loc1=new LadderBattlefieldTopListVo();
            loc1.page = 1;
            loc1.pageSize = 8;
            loc1.orderObjType = cache.ladderBattlefield.multiTabBarSelectIndex;
            loc1.onlyMyServer = false;
            loc1.showMyPos = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.LadderBattlefieldGetToplist, loc1));
            return;
        }

        /* internal  */onLadderOpenHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SLadderSchedule;
            if (cache.role.entityInfo.level >= 75) 
            {
                this.btn.addTfLeftTime(DateUtil.getSecondsDis(ClockManager.instance.nowDate, loc1.endDt));
                this.btn.show();
            }
            Dispatcher.addEventListener(EventName.ChangeScene, this.onChangeSceneHandler);
            return;
        }

        /* internal  */onLadderCloseHandler(arg1: MessageBlock): void
        {
            var e: MessageBlock;

            var loc1;
            e = arg1;
            if (this._btn) 
            {
                this.btn.hide();
            }
            if (_view && !_view.isHide) 
            {
                view.hide();
            }
            with ({}) 
            {
            };
            
#error('pushwith') {}
            .flash.utils.setTimeout(
#error('pushwith') {}
            .leave = (): void
            {
                Dispatcher.removeEventListener(EventName.ChangeScene, onChangeSceneHandler);
                return;
            }, 350000)
            return;
        }

        /* internal  */onLadderMyInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SLadderMyInfo;
            cache.ladderBattlefield.myInfo = loc1;
            view.show();
            ILadderIntegralBattlefieldModule(view).setMyInfo();
            return;
        }

        /* internal  */onLadderToplistsHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SLadderToplists;
            ILadderIntegralBattlefieldModule(view).toplist = loc1;
            return;
        }

        /* internal  */onLadderSignUpHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SLadderSignUp;
            cache.ladderBattlefield.isSignUp = !loc1.isCancel;
            ILadderIntegralBattlefieldModule(view).setSignUpBtnStyle();
            if (cache.ladderBattlefield.isSignUp) 
            {
                GangFightsWaitingTimeView.instance.show();
                GangFightsWaitingTimeView.instance.reset();
                GangFightsWaitingTimeView.instance.start();
            }
            else 
            {
                GangFightsWaitingTimeView.instance.stop();
                GangFightsWaitingTimeView.instance.hide();
            }
            return;
        }

        /* internal  */onLadderBattlesHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SLadderBattles;
            ILadderIntegralBattlefieldModule(view).battlefieldList = loc1.battles;
            return;
        }

        /* internal  */onLadderMatchHandler(arg1: MessageBlock): void
        {
            var e: MessageBlock;
            var vo: SLadderMatch;
            var onClickHandler: Function;

            var loc1;
            vo = null;
            onClickHandler = null;
            e = arg1;
            onClickHandler = (arg1: int): void
            {
                GameProxy.copy.enterLadder(false, vo.ladderId);
                return;
            }
            vo = e.messageBase as SLadderMatch;
            Alert.mode = Alert.Mode_NoneNotModal;
            Alert.alertWinRenderer = TimerAlertWin;
            Alert.needCallCloseHandler = false;
            Alert.buttonWidth = 70;
            Alert.timerOut = Math.ceil(10);
            Alert.okLabel = Language.getString(42203);
            Alert.timerOutHandler = onClickHandler;
            Alert.show(Language.getString(42205), null, Alert.OK, null, onClickHandler);
            GangFightsWaitingTimeView.instance.stop();
            GangFightsWaitingTimeView.instance.hide();
            GangFightsReadyTimeView.instance.updateLeftTime(vo.leftEnterTime);
            GangFightsReadyTimeView.instance.resetPosition();
            GangFightsReadyTimeView.instance.hide();
            return;
        }

        /* internal  */onChangeSceneHandler(arg1: DataEvent): void
        {
            if (GameMapUtil.curMapState.isLadderBattlefield && !cache.ladderBattlefield.isStart) 
            {
                this._isInBattlefield = true;
                view.hide();
                GangFightsReadyTimeView.instance.show();
                if (GameController.player.isViewShow) 
                {
                    GameController.player.view.hide();
                }
                if (GameController.skill.isViewShow) 
                {
                    GameController.skill.view.hide();
                }
                if (GameController.fashionBox.isViewShow) 
                {
                    GameController.fashionBox.view.hide();
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, false));
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            }
            else if (this._isInBattlefield && !GameMapUtil.curMapState.isLadderBattlefield) 
            {
                this._isInBattlefield = false;
                cache.ladderBattlefield.canMove = false;
                if (this._leftTimeView && !this._leftTimeView.isHide) 
                {
                    this.leftTimeView.hide();
                }
                if (this._resultWindow && !this._resultWindow.isHide) 
                {
                    this.resultWindow.hide();
                }
                GangFightsReadyTimeView.instance.hide();
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowGameMainUI, true));
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, true));
            }
            return;
        }

        /* internal  */onLadderStartHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SLadderStart;
            cache.ladderBattlefield.isStart = true;
            cache.ladderBattlefield.canMove = true;
            GangFightsReadyTimeView.instance.hide();
            this.leftTimeView.show();
            this.leftTimeView.resetPosition();
            this.leftTimeView.updateLeftTime(loc1.time);
            return;
        }

        /* internal  */onLadderResultHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SLadderResult;
            cache.ladderBattlefield.isStart = false;
            this.resultWindow.show();
            this.resultWindow.updateResult(loc1);
            return;
        }

        /* internal  */onLadderAccountHandler(arg1: MessageBlock): void
        {
            return;
        }

        /* internal  */onLadderPlayerInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SLadderPlayerInfo;
            loc1.flyLevel = cache.wardrobe.dealFlyLevel(loc1.flyLevel);
            return;
        }

        /* internal  */get resultWindow(): LadderBattlefieldResultWindow
        {
            if (!this._resultWindow) 
            {
                this._resultWindow = new LadderBattlefieldResultWindow();
            }
            return this._resultWindow;
        }

        /* internal  */get leftTimeView(): LeftTimeView
        {
            if (!this._leftTimeView) 
            {
                this._leftTimeView = new LeftTimeView(Language.getString(42208));
            }
            return this._leftTimeView;
        }

        private /* var */_btn: LadderBattlefieldIcon;

        private /* var */_module: ILadderIntegralBattlefieldModule;

        private /* var */_leftTimeView: LeftTimeView;

        private /* var */_resultWindow: LadderBattlefieldResultWindow;

        private /* var */_isInBattlefield: Boolean;
    }
